﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using IceDog.DNF.Program.WebApi2.CustomActionResults;
using IceDog.DNF.Program.WebApi2.Models;

namespace IceDog.DNF.Program.WebApi2.Controllers
{
    public class TestController : ApiController
    {

        /// <summary>
        /// 无返回体的请求
        /// </summary>
        [HttpGet]
        public void VoidRequest()
        {
        }

        /// <summary>
        /// 仅仅返回一个只有头的响应
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage HttpResponseMessageRequest()
        {
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "very OK");
            //response.Content = new StringContent("hello", Encoding.UTF8);
            response.Headers.CacheControl = new CacheControlHeaderValue()
            {
                MaxAge = TimeSpan.FromMinutes(20)
            };
            response.Headers.Add("IceDog-Tag-1", "this is a custom tag");
            return response;
        }

        /// <summary>
        /// 返回数字列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage HttpResponseMessageRequestNumberList()
        {
            // Get a list of products from a database.
            IEnumerable<int> p = Enumerable.Range(1,100);
            // Write the list to the response body.
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, p);
            return response;
        }

        /// <summary>
        /// 返回text result
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult IHttpActionResultRequest()
        {
            return new TextResult("hello", Request);
        }

        /// <summary>
        /// 返回特定id的产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult BuiltInResultsRequest(int id = 1)
        {
            string number = "666";
            if (number == null)
            {
                return NotFound(); // Returns a NotFoundResult
            }
            return Ok(number);  // Returns an OkNegotiatedContentResult
        }
    }
}
